import pygame,sys,os
from MainWindow import MainWindow

class EndScreen(MainWindow):
    def __init__(self):
        super().__init__()
        self.running = True
        self.background = pygame.Surface((self.Screen_width, self.Screen_height))
        self.background.fill((255, 171, 51))

        # 颜色定义
        self.WHITE = (255, 255, 255)
        self.BUTTON_COLOR = (0, 0, 0)
        self.BUTTON_HOVER_COLOR = (120, 120, 120)

        # 字体设置
        try:
            self.title_font = pygame.font.Font(self.resource_path("source/fonts/峰广明锐体.ttf"), 72)
            self.button_font = pygame.font.Font(self.resource_path("source/fonts/峰广明锐体.ttf"), 40)
        except:
            self.title_font = pygame.font.Font(None, 72)
            self.button_font = pygame.font.Font(None, 40)

        # 初始化界面元素
        self.init_ui()

    def init_ui(self):
        # 标题文字
        self.title_text = self.title_font.render("通关成功!", True, (196, 43, 28))
        self.title_rect = self.title_text.get_rect(center=(self.Screen_width // 2, 200))

        # 按钮参数
        btn_width, btn_height = 250, 65

        # 重新开始按钮
        self.restart_btn = pygame.Rect(
            self.Screen_width // 2 - btn_width // 2,
            400, btn_width, btn_height
        )
        self.restart_text = self.button_font.render("重新开始", True, self.WHITE)

        # 退出按钮
        self.quit_btn = pygame.Rect(
            self.Screen_width // 2 - btn_width // 2,
            500, btn_width, btn_height
        )
        self.quit_text = self.button_font.render("退出游戏", True, self.WHITE)

    def handle_events(self):
        mouse_pos = pygame.mouse.get_pos()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.quit_game()
            if super().check_mute_click(event):  # 处理静音按钮点击
                continue
            if event.type == pygame.MOUSEBUTTONDOWN:
                if self.restart_btn.collidepoint(mouse_pos):
                    return "restart"
                elif self.quit_btn.collidepoint(mouse_pos):
                    return "quit"
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return "quit"
        return "end"

    def draw(self):
        self.window.blit(self.background, (0, 0))
        self.window.blit(self.title_text, self.title_rect)

        # 绘制按钮
        mouse_pos = pygame.mouse.get_pos()

        # 重新开始按钮
        restart_color = self.BUTTON_HOVER_COLOR if self.restart_btn.collidepoint(mouse_pos) else self.BUTTON_COLOR
        pygame.draw.rect(self.window, restart_color, self.restart_btn, border_radius=10)
        self.window.blit(self.restart_text,
                         (self.restart_btn.centerx - self.restart_text.get_width() // 2,
                          self.restart_btn.centery - self.restart_text.get_height() // 2))

        # 退出按钮
        quit_color = self.BUTTON_HOVER_COLOR if self.quit_btn.collidepoint(mouse_pos) else self.BUTTON_COLOR
        pygame.draw.rect(self.window, quit_color, self.quit_btn, border_radius=10)
        self.window.blit(self.quit_text,
                         (self.quit_btn.centerx - self.quit_text.get_width() // 2,
                          self.quit_btn.centery - self.quit_text.get_height() // 2))
        self.draw_mute_button()  # 绘制静音按钮
        pygame.display.update()
    def quit_game(self):
        pygame.quit()
        sys.exit()

    def run(self):
        while True:
            action = self.handle_events()
            if action in ("restart", "quit"):
                return action  # 返回明确的操作类型
            self.draw()
            pygame.display.update()
            self.clock.tick(60)
    def resource_path(self,relative_path):
        """ 根据打包后的环境动态加载资源路径 """
        if hasattr(sys, '_MEIPASS'):
            base_path = sys._MEIPASS
        else:
            base_path = os.path.abspath(".")
        return os.path.join(base_path, relative_path)